﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.Domain.OutboundShipments;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.Domain.Storages;
using B.S.DataBase.MyProject.Domain.ViewModels;
using B.S.DataBase.MyProject.Domain.WhereHorse;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using Consul;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.PurchaseInStroeCommand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.PurchaseInStroeCommandHandler
{
    public class GetPurchaseInStoreDetailCommandHandler : IRequestHandler<GetPurchaseInStoreDetailCommand, APIResult<View_PurchaseInDetail>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<BaseStorage> storeBiz;
        private readonly IBaseRepository<Material_BasicModel> meterialModelBiz;
        private readonly IBaseRepository<PurchaseInRepository> purchaseInBiz;
        private readonly IBaseRepository<OrderMaterials> orderMaterialsBiz;

        public GetPurchaseInStoreDetailCommandHandler(IMapper mapper, IBaseRepository<BaseStorage> storeBiz, IBaseRepository<Material_BasicModel> meterialModelBiz, IBaseRepository<PurchaseInRepository> purchaseInBiz, IBaseRepository<OrderMaterials> orderMaterialsBiz)
        {
            this.mapper = mapper;
            this.storeBiz = storeBiz;
            this.meterialModelBiz = meterialModelBiz;
            this.purchaseInBiz = purchaseInBiz;
            this.orderMaterialsBiz = orderMaterialsBiz;
        }

        public Task<APIResult<View_PurchaseInDetail>> Handle(GetPurchaseInStoreDetailCommand request, CancellationToken cancellationToken)
        {
            var stores = storeBiz.GetAll().Result;
            var materialModels = meterialModelBiz.GetAll().Result;
            var purchaseIn = purchaseInBiz.Quary(x => x.Id.ToString() == request.DocumentCode).Result.FirstOrDefault();
            var orderMaterials = orderMaterialsBiz.Quary(x => x.SaleCode.ToString() == request.DocumentCode && x.MaterialsClass == 2).Result.ToList();
            APIResult<View_PurchaseInDetail> result = new APIResult<View_PurchaseInDetail>();
            result.data = new View_PurchaseInDetail
            {
                OrderMaterials = orderMaterials,
                Id = purchaseIn.Id,
                BusinessType = purchaseIn.BusinessType,
                BusinessStatus = purchaseIn.BusinessStatus,
                DocumentType = purchaseIn.DocumentType,
                DocumentStatus = purchaseIn.DocumentStatus,
                Repository = purchaseIn.Repository,
                DocumentDate = purchaseIn.DocumentDate,
                QiJianDate = purchaseIn.QiJianDate,
                Remark = purchaseIn.Remark,
                Oprator = purchaseIn.Oprator,
                ArivalNumber = purchaseIn.ArivalNumber,
                SupplyUser = purchaseIn.SupplyUser,
                ExamDate = purchaseIn.ExamDate,
                BusinessDemptName = purchaseIn.BusinessDemptName,
                BusinessDempt = purchaseIn.BusinessDempt,
                ExamUserName = purchaseIn.ExamUserName,
            };
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.Count = 1;
            return Task.FromResult(result);
        }
    }
}
